package vn.student;

import java.util.ArrayList;
import vn.student.controller.ViewContactController;
import vn.student.controller.ViewHomeController;
import vn.student.controller.ViewPlannerController;
import vn.student.controller.ViewScheduleController;
import vn.student.mylib.Constants;
import vn.student.mylib.Contact;
import vn.student.mylib.ListViewAdapter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

	/*
	
		Class name: main.java
		Function: Day la lop chinh cua chuong trinh, la lop dau tien duoc khoi tao va 
			chay khi chuong trinh duoc mo len.
		
	*/
public class main extends Activity implements View.OnClickListener{
	ViewContactController viewInsCon = null;
	ViewPlannerController viewPlannerController = null;
	ViewScheduleController viewScheduleController = null;
	RelativeLayout	homeRelativeLayout = null;
	RelativeLayout	scheduleRelativeLayout = null;
	RelativeLayout	plannerRelativeLayout = null;
	RelativeLayout	contactsRelativeLayout = null;
	/*
		Method name: public void onCreate(Bundle savedInstanceState).
		Function: Duoc chay dau tien khi ung dung duoc mo len. Ham co tac dung gan cac su kien
			cho 4 menu chinh cua chuong trinh (Home, Schedule, Planner, Contact).
		Parameter: Bundle savedInstanceState: Nhung thong tin duoc luu lai truoc khi Activity 
			chuyen tu trang thai Running qua Plaused.
		Return: None.
		Note:
    */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        

    	homeRelativeLayout = (RelativeLayout) findViewById(R.id.rl_home);
    	scheduleRelativeLayout = (RelativeLayout) findViewById(R.id.rl_schedule);
    	plannerRelativeLayout = (RelativeLayout) findViewById(R.id.rl_planner);
    	contactsRelativeLayout = (RelativeLayout) findViewById(R.id.rl_contacts);
    	
        ImageButton img_btn = (ImageButton) findViewById(R.id.btn_home);
        img_btn.setOnClickListener(this);
        img_btn = (ImageButton) findViewById(R.id.btn_schedule);
        img_btn.setOnClickListener(this);
        img_btn = (ImageButton) findViewById(R.id.btn_planner);
        img_btn.setOnClickListener(this);
        img_btn = (ImageButton) findViewById(R.id.btn_contacts);
        img_btn.setOnClickListener(this);
       	OnClickHomeButton();
    }
	/*
		Method name: public boolean onContextItemSelected(MenuItem  aItem).
		Function: Ham su ky su kien nhan cac Context menu cua chuong trinh chinh.
		Parameter: MenuItem aItem: Context menu duoc goi.
		Return: True neu su kien nhan Context Menu duoc su ly thanh cong, nguoc lai tra ve False.
		Note:
    */
    @Override
    public boolean onContextItemSelected(MenuItem  aItem) {
    	AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) aItem.getMenuInfo();
         /* Switch on the ID of the item, to get what the user selected. */
         switch (aItem.getItemId()) {
			// Su ly su kien nhan Context Menu "Add new contact"
         	case Constants.CONTACT_CONTEXTMENU_ADDNEW:
         	{
         		viewInsCon.addContact();
				return true;
         	}
              case Constants.CONTACT_CONTEXTMENU_DELETEITEM:
              {
            	  ListView listView = (ListView) findViewById(R.id.list_contact);
              	  Contact contact = ((ListViewAdapter)listView.getAdapter()).getContactInfoList().get(menuInfo.position);
            	  ListViewAdapter adapter = (ListViewAdapter)listView.getAdapter();
            	  ArrayList<Contact> tmp = adapter.getContactInfoList();
            	  AStudentDB myDB = new AStudentDB(this);
            	  if(myDB.removeContact(contact.getId()))
            			  tmp.remove(contact);
            	  adapter.setContactInfoList(tmp);
            	  listView.setAdapter(adapter);
              }
                  return true;
			// Su ly su kien nhan Context Menu "Edit contact"
              case Constants.CONTACT_CONTEXTMENU_EDITITEM:
              {
            	  ListView listView = (ListView) findViewById(R.id.list_contact);
              	  Contact contact = ((ListViewAdapter)listView.getAdapter()).getContactInfoList().get(menuInfo.position);
            	  Intent intent = new Intent(this,ViewContactDetail.class);
            	  Bundle bundle = new Bundle();
				  bundle.putString("id", contact.getId());
				  bundle.putBoolean("isEdit", true);
				  intent.putExtras(bundle);
				  this.startActivityForResult(intent,3);
              }
            	  return true;
			// Su ly su kien nhan Context Menu "Add new Semester"
              case Constants.PLANNER_CONTEXTMENU_ADD_SEMESTER:
            	  viewPlannerController.onClickAddNewSemesterButton(); 
            	  return true;
			// Su ly su kien nhan Context Menu "Delete Semester"
              case Constants.PLANNER_DETAIL_CONTEXTMENU_DELETE_SEMESTER:
            	  viewPlannerController.removeSemester(menuInfo.position);
            	  return true;
			// Su ly su kien nhan Context Menu "Edit Semester"
              case Constants.PLANNER_DETAIL_CONTEXTMENU_EDIT_SEMESTER:
            	  viewPlannerController.editSemester(menuInfo.position);
            	  return true;
         }
         return false;
    }
	/*
		Method name: public void onClick(View v).
		Function: Override phuong thuc onClick(View v) de gan cho 4 nut chinh cua chuong trinh de su ly su kin click 4 menu chinh cua chuong trinh.
		Parameter: View v: View co su kien onClick duoc goi.
		Return: None.
		Note:
    */
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btn_home:
			OnClickHomeButton();
			break;
		case R.id.btn_schedule:
			OnClickScheduleButton();	
			break;
		case R.id.btn_planner:
			OnClickPlannerButton();
			break;
		case R.id.btn_contacts:
			OnClickContactsButton();
			break;
		}
	}
	private void changeAllBtnToOff(){
		
		homeRelativeLayout.setBackgroundColor(Color.argb(00, 1, 1, 1));
		scheduleRelativeLayout.setBackgroundColor(Color.argb(00, 1, 1, 1));
		plannerRelativeLayout.setBackgroundColor(Color.argb(00, 1, 1, 1));
		contactsRelativeLayout.setBackgroundColor(Color.argb(00, 1, 1, 1));
		
	}
	/*
		Method name: private void OnClickHomeButton().
		Function: Ham su ly su kien nhan nut Home tren menu chinh cua chuong trinh.
			Ham se khoi tao doi tuoc Controller de su ly su kien o trang Home.
		Parameter: None.
		Return: None.
		Note:
    */
	private void OnClickHomeButton(){
		ViewHomeController viewHomCon = new ViewHomeController(this);
		this.changeAllBtnToOff();
		homeRelativeLayout.setBackgroundColor(Color.argb(30, 128, 128, 128));
		viewHomCon.onClickViewTodayClass();
	}
	/*
		Method name: private void OnClickScheduleButton().
		Function: Ham su ly su kien nhan nut Schedule tren menu chinh cua chuong trinh.
			Ham se khoi tao doi tuoc Controller de su ly su kien o trang Schedule.
			Xoa moi View tren man hinh chinh cua chuong trinh de duoc thay the bang giao dien luu trong file XML.
		Parameter: None.
		Return: None.
		Note:
    */
	private void OnClickScheduleButton(){
		LinearLayout lnl = (LinearLayout)findViewById(R.id.lnl_main);
		lnl.removeAllViews();
		this.changeAllBtnToOff();
		scheduleRelativeLayout.setBackgroundColor(Color.argb(30, 128, 128, 128));
		viewScheduleController = new ViewScheduleController(this);
		viewScheduleController.showSchedule();
	}
	/*
		Method name: private void OnClickPlannerButton().
		Function: Ham su ly su kien nhan nut Planner tren menu chinh cua chuong trinh.
			Ham se khoi tao doi tuoc Controller de su ly su kien o trang Planner.
			Xoa moi View tren man hinh chinh cua chuong trinh de duoc thay the bang giao dien luu trong file XML.
		Parameter: None.
		Return: None.
		Note:
    */
	private void OnClickPlannerButton(){
		LinearLayout lnl = (LinearLayout)findViewById(R.id.lnl_main);
		lnl.removeAllViews();
		this.changeAllBtnToOff();
		plannerRelativeLayout.setBackgroundColor(Color.argb(30, 128, 128, 128));
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.planner, lnl);
		viewPlannerController = new ViewPlannerController(this);
	}
	/*
		Method name: private void OnClickContactsButton().
		Function: Ham su ly su kien nhan nut Contact tren menu chinh cua chuong trinh.
			Ham se khoi tao doi tuoc Controller de su ly su kien o trang Contact.
			Xoa moi View tren man hinh chinh cua chuong trinh de duoc thay the bang giao dien luu trong file XML.
		Parameter: None.
		Return: None.
		Note:
    */
	private void OnClickContactsButton(){
		LinearLayout lnl = (LinearLayout)findViewById(R.id.lnl_main);
		lnl.removeAllViews();
		this.changeAllBtnToOff();
		contactsRelativeLayout.setBackgroundColor(Color.argb(60, 128, 128, 128));
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.contact, lnl);
		viewInsCon = new ViewContactController(this);
		viewInsCon.onClickViewInstructorContact();
	}
	/*
		Method name: protected void onActivityResult(int requestCode, int resultCode, Intent data).
		Function: Override phuong thuc onActivityResult() de bat su kien dong cac Activity duoc mo boi Activity chinh cua chuong trinh.
		Parameter: int requestCode: Ma code cua activity da goi Activity vua close, int resultCode: ket qua Activity tra ve, 
			Intent data: Doi tuong Intent chua thong tin duoc tra ve tu Activity moi duoc close.
		Return: None.
		Note:
    */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode==3) {
			//if (resultCode==3) {
				  if(viewInsCon.isViewInstructor)
					  viewInsCon.onClickViewInstructorContact();
				  else
					  viewInsCon.onClickViewStudentContact();
			//}
		}
		if(requestCode==Constants.VIEW_ADD_NEW_INSTRUCTOR_EDITOR){
			if(resultCode==RESULT_OK){
				if(viewInsCon.isViewInstructor)
					  viewInsCon.onClickViewInstructorContact();
				  else
					  viewInsCon.onClickViewStudentContact();
			}
		}
		if(requestCode==Constants.ViewClassDetailRequestCode){
			if(resultCode==RESULT_OK){
				this.OnClickHomeButton();
			}
		}
		if(requestCode==Constants.ViewAddNewSemesterDetailEditorRequestCode){
			if(resultCode==RESULT_OK){
				this.OnClickPlannerButton();
			}
		}
		if(requestCode==Constants.ViewSemesterDetailEditorRequestCode){
			if(resultCode==RESULT_OK){
				this.OnClickPlannerButton();
			}
		}
		if(requestCode==Constants.ViewAssignmentDetailInPlannerRequestCode){
			if(resultCode==RESULT_OK){
				viewPlannerController.showAssignments();
			}
		}
		if(requestCode==Constants.ViewDateScheduleRequestCode){
			if(resultCode==RESULT_OK){
				viewScheduleController.showSchedule();
			}
		}
		if(requestCode==Constants.ViewSemesterDetailRequestCode){
			if(resultCode==RESULT_OK){
				viewPlannerController.viewSemesters();
			}
		}
		if(requestCode==Constants.ViewDateScheduleRequestCode){
			if(resultCode==RESULT_OK){
				viewScheduleController.showSchedule();
			}
		}
		
	}
}